﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for Factory
/// </summary>
public static class Factory
{
    public static string emptyNull(object value)
    {
        if (value == null || value == DBNull.Value)
        {
            return "";
        }
        return Convert.ToString(value);
    }

    public static bool IsDate(object value)
    {
        DateTime x;
        if (DateTime.TryParse(Convert.ToString(value), out x))
        {
            return true;
        }
        return false;
    }

    public static string ConvertDateTime(object value)
    {
        string strDateTime = emptyNull(value).Trim();
        if (strDateTime == "")
        {
            return "";
        }
        string[] strArr = strDateTime.Split(' ');
        string strDate = "";
        string strTime = "";
        if (strArr.Length == 2)
        {
            strDate = strArr[0];
            strTime = strArr[1];
        }
        else
        {
            if (strArr.Length == 1)
            {
                strDate = strArr[0];
            }
        }
        string[] strDateArr = strDate.Split('/');
        if (strDateArr.Length == 3)
        {
            strDateTime = strDateArr[2] + "/" + strDateArr[1] + "/" + strDateArr[0] + " " + strTime;
            return strDateTime.Trim();
        }
        return "";
    }

    public static string toSQLBit(object value)
    {
        string SQLString = emptyNull(value);
        if (SQLString == "")
        {
            SQLString = "null";
        }
        else
        {
            if (SQLString.ToUpper() == "true".ToUpper() || SQLString == "1")
            {
                SQLString = "1";
            }
            else
            {
                SQLString = "0";
            }
        }
        return SQLString;
    }

    public static bool IsInt(string so)
    {
        int intso;
        try
        {
            intso = Convert.ToInt32(so);
            return true;
        }
        catch
        {
            return false;
        }

    }

    public static bool IsDouble(string so)
    {
        double doubleso;
        try
        {
            doubleso = Convert.ToDouble(so);
            return true;
        }
        catch
        {
            return false;
        }

    }

    public static int wordCountSimple(string ToCount)
    {
        return ToCount.Split('|').Length;

    }

    /// <summary>
    /// Count words (using spaces and character analysis).
    /// </summary>
    /// <param name="str">The string to count the words of.</param>
    /// <returns>The number of words in the document.</returns>
    public static int WordCount(string str)
    {
        int c = 0;
        for (int i = 1; i < str.Length; i++)
        {
            if (char.IsWhiteSpace(str[i - 1]) == true)
            {
                if (char.IsLetterOrDigit(str[i]) == true ||
                    char.IsPunctuation(str[i]))
                {
                    c++;
                }
            }
            else if (i >= 3 &&
                char.IsLetterOrDigit(str[i]) &&
                char.IsPunctuation(str[i - 1]) &&
                char.IsPunctuation(str[i - 2]) &&
                char.IsLetterOrDigit(str[i - 3]))
            {
                c++;
            }
        }
        if (str.Length > 2)
        {
            c++;
        }
        return c;
    }

    /// <summary>
    /// Count the number of words in the string using regular expression.
    /// </summary>
    /// <param name="textIn">The string you want a word count of.</param>
    /// <returns>The number of words in the string.</returns>
    public static int CountRegex(string textIn)
    {
        MatchCollection collection = Regex.Matches(textIn, @"[\S]+");
        return collection.Count;
    }

    public static void thongBao(Literal ltrThongBao, string sMsg)
    {
        ltrThongBao.Text = "\n <script > " +
            "\n				alert('" + sMsg + "\n'); " +
            "\n</script> ";
    }

    public static string DoiCode128B(string chuoi)
    {
        string returnstring = "";
        //returnstring=chuoi.ToUpper();
        char[] ch = chuoi.ToCharArray();
        int sum = 104;
        for (int i = 0; i < ch.Length; i++)
        {
            returnstring += ((int)ch[i] - 32).ToString() + " * " + (i + 1) + " = " + ((int)ch[i] - 32) * (i + 1) + " + ";
            sum += ((int)ch[i] - 32) * (i + 1);
        }
        //		message.Text="SUM: "+sum+"checkdigit: sum % 103 = "+sum%103+" check char: ";
        char chch = ((char)(sum % 103 + 32));
        int[] avchar = { 0, 95, 96, 97, 98, 99, 100, 101, 102 };
        int[] a128char = { 223, 180, 228, 246, 252, 196, 214, 220, 181 };
        int ichch = ((int)chch) - 32;
        int i128chch = ichch;
        char c128chch = chch;
        if (ichch >= 95 || ichch == 0)
        {
            for (int i = 0; i < avchar.Length; i++)
            {
                if (ichch == avchar[i])
                {
                    i128chch = a128char[i];
                    c128chch = (char)i128chch;
                }
            }
        }
        //		message.Text+=c128chch;
        //		message.Text+=" Converted String: "+"Á"+chuoi+c128chch+"È";
        //	codestr.Text=returnstring+" sum: "+sum;
        string c128returnstring = "Á" + chuoi + c128chch + "È";
        return c128returnstring;
        //	char[] valueB={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106"};
        //	char[] codeB={"SP","!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","I","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","DEL","FNC 3","FNC 2","SHIFT","CODE C","FNC 4","CODE A","FNC 1","Start A","Start B","Start C","Stop"};


    }
}